<ui:composition template = "template.xhtml"
                xmlns ="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:rich="http://richfaces.org/rich"
                xmlns:a4j="http://richfaces.org/a4j"
                xmlns:sdh="http://richfaces.org/session-data-helper"
                xmlns:c="http://java.sun.com/jsp/jstl/core">
    <ui:define name ="conteudo">

        <h:form id="lista">

            <h:dataTable value="#{novoRegistroBean.registro.conteudo}" var="cont" >

                <h:column>
                    <h:outputText value="#{cont.campo.titulo}" />                   
                </h:column>

                <h:column> 

                    <h:panelGroup class="data" rendered="#{cont.campo.tipoCampo.chaveTipoCampo eq 1}">
                        <rich:calendar datePattern="dd/MM/yyyy" value="#{cont.valor}" converter="CalDateStrConveter" converterMessage="Data invalida" mode="client" required="#{cont.obrigatorio}" >
                        </rich:calendar>
                    </h:panelGroup>

                    <h:panelGroup rendered="#{cont.campo.tipoCampo.chaveTipoCampo eq 2 and cont.valor eq null}">
                        <h:form class="documento" enctype="multipart/form-data">
                            <rich:fileUpload fileUploadListener="#{novoRegistroBean.uploadListener}" id="upload" maxFilesQuantity="1" acceptedTypes="doc, docx, pdf" ontyperejected="alert('Somente arquivos doc, docx e pdf')" style="width:100%;height:72px">
                                <f:ajax event="uploadcomplete" onevent="salvaDoc()" execute="@form" render=":lista" />
                            </rich:fileUpload>
                            <a4j:jsFunction id="js1" name="salvaDoc" action="#{novoRegistroBean.manuseiaDadosUpload(cont)}" />
                        </h:form>
                    </h:panelGroup>

                    <h:panelGroup rendered="#{cont.campo.tipoCampo.chaveTipoCampo eq 3 and cont.valor eq null}">
                        <h:form class="imagem" enctype="multipart/form-data">
                            <rich:fileUpload fileUploadListener="#{novoRegistroBean.uploadListener}" id="upload" maxFilesQuantity="1" acceptedTypes="jpg, gif, png, bmp" ontyperejected="alert('Somente arquivos jpg, gif, png e bmp')" style="width:100%;height:72px">
                                <f:ajax event="uploadcomplete" onevent="salvaImg()" execute="@form" render=":lista" />
                            </rich:fileUpload>
                            <a4j:jsFunction id="js1" name="salvaImg" action="#{novoRegistroBean.manuseiaDadosUpload(cont)}" />
                        </h:form>
                    </h:panelGroup>

                    <h:panelGroup class="link" rendered="#{cont.campo.tipoCampo.chaveTipoCampo eq 4}">
                        <h:inputText value="#{cont.valor}" required="#{cont.obrigatorio}"/>
                    </h:panelGroup>

                    <h:panelGroup class="metadado" rendered="#{cont.campo.tipoCampo.chaveTipoCampo eq 5}">

                        <rich:autocomplete id="buscaCont" 
                                           autocompleteMethod="#{novoRegistroBean.autocomplete}"
                                           minChars="3" 
                                           required="#{cont.obrigatorio}"
                                           var="conteudo" showButton="true"
                                           onfocus="pegaChaveObj(#{cont.campo.chaveCampo})"
                                           fetchValue="#{conteudo.chaveConteudo}">
                            <h:outputText value="#{conteudo.valor}" />
                            <f:ajax event="selectitem" execute="@this" immediate="true" onevent="valorConteudo(this.value)" render="divRegistro"/>
                        </rich:autocomplete>

                        <a4j:jsFunction name="valorConteudo">
                            <a4j:param name="param1" assignTo="#{novoRegistroBean.oi}" />
                        </a4j:jsFunction>

                        <a4j:jsFunction name="pegaChaveObj">
                            <a4j:param name="param1" assignTo="#{novoRegistroBean.chaveCampoAux}" />
                        </a4j:jsFunction>

                        <h:panelGrid id="divRegistro">
                            <ui:repeat value="#{novoRegistroBean.registroAux.conteudo}" var="reg" rendered="#{novoRegistroBean.oi ne null}">
                                <h:panelGroup rendered="#{novoRegistroBean.ehApresentacao(cont, reg)}">
                                    <h:outputText value="#{reg.campo.titulo}: " style="font-size: 10px; color: #0000c6" />
                                    <h:outputText value="#{reg.valor}" style="font-size: 10px;" rendered="#{reg.campo.chaveCampo ne 71}" />
                                    <h:outputLink value="http://#{reg.valor}" target="_blank" style="font-size: 10px;" rendered="#{reg.campo.chaveCampo eq 71}" >
                                        <h:outputText value="#{reg.valor}" />
                                    </h:outputLink> <br />
                                </h:panelGroup>
                            </ui:repeat>
                            <h:link value="Ir para registro" target="_blank" outcome="mostraRegistro" rendered="#{novoRegistroBean.oi ne null}" >
                                <f:param name="chaveReg" value="#{novoRegistroBean.registroAux.registro}" />
                            </h:link>
                        </h:panelGrid>

                    </h:panelGroup>

                    <h:panelGroup rendered="#{cont.campo.tipoCampo.chaveTipoCampo eq 6 and cont.valor eq null}">
                        <h:form class="multimidia" enctype="multipart/form-data">
                            <rich:fileUpload fileUploadListener="#{novoRegistroBean.uploadListener}" id="upload" maxFilesQuantity="1" acceptedTypes="avi, mpg, wmv" ontyperejected="alert('Somente arquivos avi, mpg, wmv')" style="width:100%;height:72px"/>
                        </h:form>
                    </h:panelGroup>

                    <h:panelGroup class="numero" rendered="#{cont.campo.tipoCampo.chaveTipoCampo eq 7}">
                        <h:inputText id="numero" value="#{cont.valor}" validatorMessage="Digite um número entre 1 e 500" required="#{cont.obrigatorio}"
                                     onkeypress="if(event.which &lt; 48 || event.which &gt; 57) return false;">
                            <f:validateLongRange minimum="1" maximum="500" />
                            <rich:validator />
                        </h:inputText>
                        <rich:message for="numero" />
                    </h:panelGroup>

                    <h:panelGroup class="textoCurto" rendered="#{cont.campo.tipoCampo.chaveTipoCampo eq 8}">
                        <h:panelGroup rendered="#{cont.restricao eq null}" >
                            <h:inputText id="textoCurto" value="#{cont.valor}" size="57" validatorMessage="Digite até 199 caracteres" required="#{cont.obrigatorio}">
                                <f:validateLength minimum="1" maximum="199" />
                                <rich:validator />
                            </h:inputText>
                        </h:panelGroup>
                        <h:panelGroup rendered="#{cont.restricao ne null}" >
                            <h:selectOneMenu value="#{cont.valor}">
                                <f:selectItems value="#{novoRegistroBean.listaRestricao(cont)}" var="item" itemLabel="#{item}" itemValue="#{item}" />
                            </h:selectOneMenu>
                        </h:panelGroup>
                        <rich:message for="textoCurto" />
                    </h:panelGroup>

                    <h:panelGroup class="textoLongo" rendered="#{cont.campo.tipoCampo.chaveTipoCampo eq 9}">
                        <rich:editor value="#{cont.valor}" onblur="salvaText()" required="#{cont.obrigatorio}">
                            <f:ajax event="dirty" render="@this" />
                        </rich:editor>
                        <a4j:jsFunction id="js1" name="salvaText" action="#{novoRegistroBean.manuseiaDadosUpload(cont)}" />
                    </h:panelGroup>

                    <h:panelGroup class="consultaSQL" rendered="#{cont.campo.tipoCampo.chaveTipoCampo eq 10}">
                        <h:inputText value="#{cont.valor}"/>
                    </h:panelGroup>

                </h:column>

                <h:column>
                    <h:commandLink action="#{novoRegistroBean.addRow}" rendered="#{cont.repetitivo eq true}">
                        <h:graphicImage library="imagens/icones" name="add.png" />
                        <f:setPropertyActionListener target="#{novoRegistroBean.conteudoAux}" value="#{cont}" />
                        <f:ajax execute="@form" render=":lista" />
                    </h:commandLink>
                </h:column>

                <h:column>
                    <h:commandLink action="#{novoRegistroBean.delRow}">
                        <h:graphicImage library="imagens/icones" name="delete.png" />
                        <f:setPropertyActionListener target="#{novoRegistroBean.conteudoAux}" value="#{cont}" />
                        <f:ajax execute="@form" render=":lista" />
                    </h:commandLink>
                </h:column>

            </h:dataTable>

            <h:commandButton value="Salvar" >
                <f:ajax
                    execute ="@form"
                    listener ="#{novoRegistroBean.create()}"
                    render ="@all" />
            </h:commandButton>
            <h:commandButton value="Cancela" />

        </h:form>

    </ui:define>
</ui:composition>
